﻿using System;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using StructureMap;

namespace ECommerce.Web.ControllerFactories
{
    public class ServiceLocatorControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new HttpException((int)HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToString());
            }

            return (IController)ObjectFactory.GetInstance(controllerType);
        }

        public override void ReleaseController(IController controller)
        {
            IDisposable disposable = controller as IDisposable;

            if (disposable != null)
            {
                disposable.Dispose();
            }

            base.ReleaseController(controller);
        }
    }
}
